Skip to content

Migrate region discovery to IMDS /compute JSON endpoint#929

Merged
Robbie-Microsoft merged 4 commits into
devfrom
rginsburg/imds_compute_json
Jun 22, 2026
Merged

Migrate region discovery to IMDS /compute JSON endpoint#929
Robbie-Microsoft merged 4 commits into
devfrom
rginsburg/imds_compute_json

Conversation

@Robbie-Microsoft

@Robbie-Microsoft Robbie-Microsoft commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Ports microsoft-authentication-library-for-dotnet#6057 to MSAL Python.

What

Migrates Azure VM region auto-detection from the legacy IMDS text endpoint to the newer JSON endpoint.

Details

  • msal/region.py _detect_region_of_azure_vm: updated URL (dropped /location route param and format=text, bumped api-version to 2021-02-01) and now parses json.loads(resp.text).get("location").
  • Missing/null location and malformed JSON funnel into the existing fallback (return None); the network-failure path and _validate_region are unchanged.
  • No public API, telemetry, or config changes. REGION_NAME env-var precedence preserved.

Tests

Added TestDetectRegionOfAzureVm to ests/test_region.py: valid location, missing/null location, malformed JSON, invalid region value, network failure, and an assertion on the new request URL/headers. All 20 tests in ests/test_region.py pass.

Copilot AI review requested due to automatic review settings June 18, 2026 01:07
@Robbie-Microsoft Robbie-Microsoft requested a review from a team as a code owner June 18, 2026 01:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR migrates Azure VM region auto-detection in MSAL Python from the legacy IMDS text endpoint (/compute/location?format=text) to the newer IMDS JSON compute endpoint (/compute), aligning with the newer metadata API behavior while preserving existing region validation and fallback behavior.

Changes:

  • Updated _detect_region_of_azure_vm to call the IMDS /compute?api-version=2021-02-01 endpoint and extract location from the JSON payload.
  • Added unit tests covering valid/missing/null/malformed location, invalid region values, network failure, and the updated request URL/headers.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
msal/region.py Switches IMDS endpoint to /compute JSON response and parses location for region validation.
tests/test_region.py Adds focused tests to validate the new IMDS URL/headers and JSON parsing fallback behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread msal/region.py
Comment thread msal/region.py Outdated
Copilot AI review requested due to automatic review settings June 18, 2026 19:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread msal/region.py
@Robbie-Microsoft

Copy link
Copy Markdown
Contributor Author

/azp run MSAL.Python-PR-OneBranch-Official

1 similar comment
@Robbie-Microsoft

Copy link
Copy Markdown
Contributor Author

/azp run MSAL.Python-PR-OneBranch-Official

@RyAuld

RyAuld commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

@copilot rebase

Copilot AI commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

@RyAuld Unfortunately I hit an unexpected error while processing your comment. I've automatically reported this to GitHub.

You can ask me to try again later by mentioning me in a new comment.

If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: 04a6a70c-74a4-46d2-8b3e-dddcd4b3469b

Sorry for the inconvenience!

Robbie-Microsoft and others added 3 commits June 22, 2026 12:10
Switch Azure VM region auto-detection from the legacy IMDS text endpoint
(/metadata/instance/compute/location?format=text) to the JSON endpoint
(/metadata/instance/compute?api-version=2021-02-01), reading the region
from the 'location' field. Missing/null location and malformed JSON fall
back to None as before.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Treat a non-string resp.text (json.loads raising TypeError) and a
non-string location field as malformed IMDS responses and fall back to
None, so region auto-detection cannot raise during client setup.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Move the IMDS compute metadata api-version into a module-level
_IMDS_API_VERSION constant so future version bumps are a single,
self-documenting change, consistent with msal-go and msal-java.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@RyAuld RyAuld force-pushed the rginsburg/imds_compute_json branch from 41346a3 to cb0c5ef Compare June 22, 2026 19:10

Copilot AI commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

@copilot rebase

The rebase completed locally (3 commits cleanly on top of dev tip bfd7fb7, merge commit removed), but the push was rejected because it requires a force-push — which isn't supported by my push tooling. You can complete the rebase by running these commands locally:

git fetch origin
git checkout rginsburg/imds_compute_json
git rebase origin/dev
git push --force-with-lease

Copilot AI requested a review from RyAuld June 22, 2026 19:11
Copilot AI review requested due to automatic review settings June 22, 2026 20:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@Robbie-Microsoft Robbie-Microsoft merged commit 55dd09f into dev Jun 22, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants